package com.ruoyi.web.controller.qsng;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.qsng.domain.KAgeGroup;
import com.ruoyi.qsng.domain.KCourse;
import com.ruoyi.qsng.service.IKAgeGroupService;
import com.ruoyi.qsng.service.IKCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程信息Controller
 *
 * @author kanglou
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/qsng/course")
@Api(tags = "pc--课程信息Controller")
public class KCourseController extends BaseController {
    @Autowired
    private IKCourseService kCourseService;

    @Autowired
    private IKAgeGroupService kAgeGroupService;
    /**
     * 查询课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('qsng:course:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询课程信息列表")
    public TableDataInfo list(KCourse kCourse) {
        startPage();
        List<KCourse> list = kCourseService.selectKCourseList(kCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('qsng:course:export')")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出课程信息列表")
    public void export(HttpServletResponse response, KCourse kCourse) {
        KAgeGroup kAgeGroup =new KAgeGroup();
        List<KAgeGroup> selectKAgeGroupList = kAgeGroupService.selectKAgeGroupList(kAgeGroup);

        List<KCourse> list = kCourseService.selectKCourseList(kCourse);
        ExcelUtil<KCourse> util = new ExcelUtil<KCourse>(KCourse.class);
        for (KCourse kCourse1:list) {
            kCourse1.setRemainingQuota2(Math.toIntExact(kCourse1.getTotalQuota() - kCourse1.getSold()));
            //招生年龄
            for (KAgeGroup kAgeGroup1:selectKAgeGroupList) {
                if(kAgeGroup1.getId().equals(kCourse1.getAgeGroup()) ){
                    kCourse1.setAgeApp(kAgeGroup1.getName());
                    break;
                }
            }
        }
        util.exportExcel(response, list, "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('qsng:course:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取课程信息详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(kCourseService.selectKCourseById(id));
    }

    /**
     * 新增课程信息
     */
    @PreAuthorize("@ss.hasPermi('qsng:course:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增课程信息")
    public AjaxResult add(@RequestBody KCourse kCourse)
    {
        return toAjax(kCourseService.insertKCourse(kCourse));
    }

    /**
     * 修改课程信息
     */
    @PreAuthorize("@ss.hasPermi('qsng:course:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改课程信息")
    public AjaxResult edit(@RequestBody KCourse kCourse) {
        return toAjax(kCourseService.updateKCourse(kCourse));
    }

    /**
     * 删除课程信息
     */
    @PreAuthorize("@ss.hasPermi('qsng:course:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除课程信息")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(kCourseService.deleteKCourseByIds(ids));
    }
}
